package gui.components.JXTable.test;

import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.security.*;

/**
 * User: Programmer
 * Date: 15.05.2006
 * Time: 19:53:07
 */
public class testGuard3{

    Permission perm = new FilePermission("c:\\test.txt", "write");

    class myPol extends Policy {
        Permissions perms = new Permissions();
        Permission perm = null;
        myPol(){
            perm = new FilePermission("c:\\test.txt","read");
            //perms.add(new FilePermission("c:\\test.txt","write"));
            perms.add(perm);
        }

        public boolean implies(ProtectionDomain domain, Permission permission) {
            return super.implies(domain, permission);
        }

        public PermissionCollection getPermissions(CodeSource codesource){return perms;}
        public void refresh(){}
    }


    @Deprecated
    public testGuard3(){
        Runnable rnn = new Consumer();
        Runnable rpp = new Producer();
        Thread tprod = new Thread(rpp);
        Thread tcons = new Thread(rnn);
        ((Consumer)rnn).link((Producer)rpp);
        tprod.start();
        tcons.start();
        Policy.setPolicy(new myPol());
        System.setSecurityManager(new SecurityManager());
    }

    public static class Consumer implements Runnable{
        private Producer m_producer = null;

        public void link(Producer producer){
            if (m_producer != producer){
                m_producer = producer;
                m_producer.link(this);
            }
        }

        public void run(){
            for (int i = 0; i < 10; i++){
                Object object;
                object = m_producer.retrieveLocation().getObject();
                try {
                    ((FileOutputStream)object).write(666);
                    ((FileOutputStream)object).close();}
                catch (IOException e){e.printStackTrace();}
            }
        }
    }

    public static class Producer implements Runnable{
        private Consumer m_consumer = null;

        public void link(Consumer consumer){
            if (m_consumer != consumer){
                m_consumer = consumer;
                m_consumer.link(this);
            }
        }

        private String m_stringLocation = null;

        public synchronized GuardedObject retrieveLocation(){
            String stringLocation = null;
            try{
                while (m_stringLocation == null) wait();
                stringLocation = m_stringLocation;
                //System.out.println("STR: "+stringLocation);
                m_stringLocation = null;
                notifyAll();
            }
            catch (InterruptedException interruprtedexception){}
            try{
                return new GuardedObject(new FileOutputStream(stringLocation), new FilePermission(stringLocation, "write"));
            }
            catch (IOException ioexception){ioexception.printStackTrace();}
            return null;
            //javax.swing.JFormattedTextField
        }

        private synchronized void storeLocation(String stringLocation) throws InterruptedException{
            while (m_stringLocation != null) wait();
            m_stringLocation = stringLocation;
            notifyAll();
        }

        public void run(){
            while (true){
                try{
                    storeLocation("c:\\test.txt");
                }
                catch (InterruptedException interruptedexception){break;}
            }
        }
    }

    public static void main(String[] args) {
        new testGuard3();
    }
}
